﻿using System;
using System.Collections.Generic;
using System.Windows.Automation;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using QuickUIT.Controls;

namespace QuickUIT.Tests
{
    [TestClass]
    public class UITreeTests
    {
        private BaseState _baseState;

        [TestInitialize]
        public void TestSetup()
        {
            _baseState = new BaseState(TestUtilities.GetTestApplicationFileName(), "@Name='QuickUIT Test Application'");
            _baseState.Execute();
        }

        [TestMethod]
        public void ShouldSelectItemUsingTreePathFromTreeControl()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UITree treeControl = window.UITree("@AutomationId='treeView1'");
            treeControl.SelectItem(new[] {"Tree Item 2", "Tree Sub Item 1"});
            AutomationElement selection1 = treeControl.GetSelectedItem();
            Assert.AreEqual("Tree Sub Item 1", selection1.Current.Name);
        }

        [TestMethod]
        public void ShouldSelectNothingForEmptyPathOnSelectItem()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UITree treeControl = window.UITree("@AutomationId='treeView1'");
            treeControl.SelectItem(new string[] {});
            AutomationElement[] selection = treeControl.GetSelection();
            Assert.AreEqual(0, selection.Length);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void ShouldThrowArgumentNullExceptionOnSelectItem()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UITree treeControl = window.UITree("@AutomationId='treeView1'");
            treeControl.SelectItem(null);
        }

        [TestMethod]
        public void ShouldGetSelection()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UITree treeControl = window.UITree("@AutomationId='treeView1'");
            AutomationElement[] selection = treeControl.GetSelection();
            Assert.AreEqual(0, selection.Length);

            treeControl.SelectItem(new[] {"Tree Item 2"});
            selection = treeControl.GetSelection();
            Assert.AreEqual(1, selection.Length);
        }

        [TestMethod]
        public void ShouldGetCanSelectMultiple()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UITree treeControl = window.UITree("@AutomationId='treeView1'");
            Assert.IsFalse(treeControl.CanSelectMultiple);
        }

        [TestMethod]
        public void ShouldGetIsSelectionRequired()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UITree treeControl = window.UITree("@AutomationId='treeView1'");
            Assert.IsFalse(treeControl.IsSelectionRequired);
        }

        [TestMethod]
        public void ShouldGetSelectedItem()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UITree treeControl = window.UITree("@AutomationId='treeView1'");
            AutomationElement selection = treeControl.GetSelectedItem();
            Assert.IsNull(selection);

            treeControl.SelectItem(new[] { "Tree Item 2" });
            selection = treeControl.GetSelectedItem();
            Assert.IsNotNull(selection);
            Assert.AreEqual("Tree Item 2", selection.Current.Name);
        }

        [TestMethod]
        public void ShouldGetAllTrees()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            List<UITree> trees = window.UITrees();
            Assert.AreEqual(1, trees.Count);
            Assert.AreEqual("treeView1", trees[0].AutomationId);
        }

        [TestMethod]
        public void ShouldGetVerticalScrollPercent()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UITree treeControl = window.UITree("@AutomationId='treeView1'");
            Assert.AreEqual(ScrollPattern.NoScroll, treeControl.VerticalScrollPercent);
        }

        [TestMethod]
        public void ShouldGetHorizontalScrollPercent()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UITree treeControl = window.UITree("@AutomationId='treeView1'");
            Assert.AreEqual(ScrollPattern.NoScroll, treeControl.HorizontalScrollPercent);
        }

        [TestMethod]
        public void ShouldGetVerticalViewSize()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UITree treeControl = window.UITree("@AutomationId='treeView1'");
            Assert.AreEqual(100, treeControl.VerticalViewSize);
        }

        [TestMethod]
        public void ShouldGetHorizontalViewSize()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UITree treeControl = window.UITree("@AutomationId='treeView1'");
            Assert.AreEqual(100, treeControl.HorizontalViewSize);
        }

        [TestMethod]
        public void ShouldGetHorizontallyScollable()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UITree treeControl = window.UITree("@AutomationId='treeView1'");
            Assert.IsFalse(treeControl.HorizontallyScrollable); 
        }

        [TestMethod]
        public void ShouldGetVerticallyScollable()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UITree treeControl = window.UITree("@AutomationId='treeView1'");
            Assert.IsFalse(treeControl.VerticallyScrollable);
        }

        [TestMethod]
        public void ShouldScroll()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UITree treeControl = window.UITree("@AutomationId='treeView1'");
            treeControl.UITreeItem("@Name='Tree Item 1'").Expand();
            treeControl.UITreeItem("@Name='Tree Item 2'").Expand();
            Assert.IsTrue(treeControl.VerticallyScrollable);
            treeControl.Scroll(ScrollAmount.NoAmount, ScrollAmount.LargeDecrement);
        }

        [TestMethod]
        public void ShouldScrollVertically()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UITree treeControl = window.UITree("@AutomationId='treeView1'");
            treeControl.UITreeItem("@Name='Tree Item 1'").Expand();
            treeControl.UITreeItem("@Name='Tree Item 2'").Expand();
            Assert.IsTrue(treeControl.VerticallyScrollable);
            treeControl.ScrollVertically(ScrollAmount.LargeDecrement);
        }

        [TestMethod]
        public void ShouldSetScrollPercent()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UITree treeControl = window.UITree("@AutomationId='treeView1'");
            treeControl.UITreeItem("@Name='Tree Item 1'").Expand();
            treeControl.UITreeItem("@Name='Tree Item 2'").Expand();
            Assert.IsTrue(treeControl.VerticallyScrollable);
            treeControl.SetScrollPercent(ScrollPattern.NoScroll, 100);
            Assert.AreEqual(100, treeControl.VerticalScrollPercent); 
        }

        [TestMethod]
        [ExpectedException(typeof(InvalidOperationException))]
        public void ShouldThrowExceptionOnScrollHorizontallyForUnsupportedScrollDirection()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UITree treeControl = window.UITree("@AutomationId='treeView1'");
            treeControl.ScrollHorizontally(ScrollAmount.LargeIncrement);
        }

        [TestMethod]
        public void ShouldScrollToTop()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UITree treeControl = window.UITree("@AutomationId='treeView1'");
            treeControl.UITreeItem("@Name='Tree Item 1'").Expand();
            treeControl.UITreeItem("@Name='Tree Item 2'").Expand();
            Assert.IsTrue(treeControl.VerticallyScrollable);
            treeControl.SetScrollPercent(ScrollPattern.NoScroll, 100);
            Assert.AreEqual(100, treeControl.VerticalScrollPercent);
            treeControl.ScrollToTop();
            Assert.AreEqual(0, treeControl.VerticalScrollPercent);
        }

        [TestCleanup]
        public void TestTeardown()
        {
            _baseState.Cleanup();
        }
    }
}